package com.yami.shop.mystery.platform.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.param.SendUserDropsParam;
import com.yami.shop.bean.param.UserUpdateParam;
import com.yami.shop.mystery.common.model.ActivityPresell;
import com.yami.shop.mystery.common.model.ActivityProd;
import com.yami.shop.mystery.common.model.InviteRecord;
import com.yami.shop.mystery.common.service.ActivityPresellService;
import com.yami.shop.mystery.common.service.ActivityProdService;
import com.yami.shop.mystery.common.service.InviteRecordService;
import com.yami.shop.service.BusinessNftService;
import com.yami.shop.service.ShopDetailService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
@AllArgsConstructor
public class GrantTask {

    @Autowired
    private InviteRecordService inviteRecordService;
    @Autowired
    private ActivityPresellService activityPresellService;
    @Autowired
    private ActivityProdService activityProdService;
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private BusinessNftService businessNftService;

    @XxlJob("grantTask")
    public void grantTask(){
        // 获取需要发放 奖励 的数据
        QueryWrapper<InviteRecord> inviteRecordQueryWrapper = new QueryWrapper<>();
        inviteRecordQueryWrapper.ne("status",1);
        List<InviteRecord> inviteRecords = inviteRecordService.list(inviteRecordQueryWrapper);
        inviteRecords.forEach(s->{
            // 当前只存在 空投 商品
            ActivityPresell activityPresell = activityPresellService.getById(s.getActivityId());
            List<ActivityProd> activityProds = activityProdService.listByActivityId(s.getActivityId());
            // activityPresell.getBlindType() -- 0 商品 1 盲盒
            UserUpdateParam param = new UserUpdateParam();
            // 商品信息
            List<SendUserDropsParam> userDropsParams = new ArrayList<>();
            activityProds.forEach(prod ->{
                SendUserDropsParam sendUserDropsParam = new SendUserDropsParam();
                sendUserDropsParam.setTargetId(prod.getProdId());
                sendUserDropsParam.setType("1".equals(activityPresell.getBlindType()) ? 1 : 0);
                sendUserDropsParam.setNums(1);
                userDropsParams.add(sendUserDropsParam);
            });
            param.setDrops(userDropsParams);
            // User
            param.setUserIds(Collections.singletonList(s.getUserId()));
            // 权益空投类型
            param.setBusinessType(4);
            // 业务ID
            param.setAirdropBusinessId(s.getId());
            ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(activityPresell.getShopId());
            businessNftService.bindDropById(param, shopDetail);
        });
    }

}
